package com.oc.mba.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.OcTopicDAO;
import com.oc.mba.entity.OcTopic;
import com.oc.mba.util.CommLog;

@Repository
public class OcTopicDAOImpl extends BaseDAOHibernate implements OcTopicDAO {
	
	private static final CommLog logger = new CommLog(OcTopicDAOImpl.class);
	
	public List<OcTopic> findByProperty(String propertyName, Object value) {
		logger.debug("finding OcTopic instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from OcTopic as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		}
		catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public OcTopic findTopic(Integer topicID) {
		List<OcTopic> topicList = findByProperty("id", topicID);
		if (topicList != null && topicList.size() > 0) {
			return topicList.get(0);
		}
		return null;
	}

	@Override
	public List<OcTopic> listAll() {
		logger.debug("finding OcTopic list");
		try {
			String queryString = "from OcTopic as model ";
			return getHibernateTemplate().find(queryString, null);
		}
		catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}

	@Override
	public void save(OcTopic topic) {
		getHibernateTemplate().save(topic);
	}

	@Override
	public void update(OcTopic topic) {
		getHibernateTemplate().update(topic);
	}


	@Override
	public void deleteTopic(Integer topicID) {
		OcTopic topic = findTopic(topicID);
		if (topic != null) {
			getHibernateTemplate().delete(topic);
		}
	}

	@Override
	public List<OcTopic> findTopicByPaperID(Integer paperID) {
		return findByProperty("paperId", paperID);
	}
}
